package com.sacc.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.power.common.model.CommonResult;
import com.sacc.mapper.InfographicMapper;
import com.sacc.pojo.Infographic;
import com.sacc.service.InfographicService;
import com.sacc.util.FileUtil;
import com.sacc.util.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 滚~韬
 * @date 2022/4/7 20:10
 */
@Service
@Slf4j
public class InfographicServiceImpl implements InfographicService {
    @Autowired
    private FileUtil fileUtil;
    @Autowired
    private InfographicMapper infographicMapper;

    private final Logger logger = LoggerFactory.getLogger(InfographicServiceImpl.class);

    /**
     * 上传一个文件
     * @param file
     */
    @Override
    public CommonResult Upload(MultipartFile file,Integer state) {
        Infographic infographic = new Infographic();
        CommonResult result = OssUtil.Upload(file);
        infographic.setImageUrl((String) result.getData());
        infographic.setCreateTime(LocalDateTime.now());
        infographic.setState(state);
        try{
            infographicMapper.insert(infographic);
            return result;
        }catch (Exception e){
            return new CommonResult(false,"上传失败",null);
        }
    }

    /**
     * 上传多个文件
     * @param files(不要使用)
     */
    @Override
    @Deprecated
    public void Upload(MultipartFile[] files) {
        Infographic infographic = new Infographic();
        List<String> image = fileUtil.uploads("/infographic/",files,"image");
        infographic.setCreateTime(LocalDateTime.now());
        for (String s: image)
        {
            infographic.setImageUrl(s);
            infographicMapper.insert(infographic);
        }
    }

    /**
     * 删除一个文件(根据url)
     * @param url
     */
    @Deprecated
    @Override
    public CommonResult Delete(String url){
        QueryWrapper<Infographic> infographicQueryWrapper = new QueryWrapper<>();
        infographicQueryWrapper.eq("image_url",url);
        try {
            infographicMapper.delete(infographicQueryWrapper);
            return OssUtil.Delete(url);
        }catch (Exception e){
            return new CommonResult(false,"删除失败",null);
        }
    }
    /**
     * 删除一个文件(根据id)
     * @param id
     */
    @Override
    public CommonResult Delete(Integer id){
        Infographic infographic = infographicMapper.selectById(id);
        if(infographic!=null){
            infographicMapper.deleteById(id);
            return OssUtil.Delete(infographic.getImageUrl());
        }
        else {
            log.info("没找到对象");
            return new CommonResult(false,"删除失败，没找到对象",null);
        }
    }

    /**
     * 获取轮播图的图片
     * @param
     * @return
     */
    @Override
    public List<Infographic> GetAll(Integer state){
//        List<String> image = new ArrayList<>();//存放图片的base64编码
        List<Infographic> infographics = infographicMapper//找到前5个图片
                .selectList(new QueryWrapper<Infographic>()
                        .eq("state",state)
                        .select("image_url")
                        .orderByDesc("create_time"));
        return infographics;
    }
}
